package world.criver.h50529.controller;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import world.criver.h50529.application.appAffirmPop;
import world.criver.h50529.data.daControllers;
import world.criver.h50529.data.daData;
import world.criver.h50529.data.daFxml;
import world.criver.h50529.function.funFiles;
import world.criver.h50529.module.moEvent;
import world.criver.h50529.module.moListen;

import java.net.URL;
import java.util.ResourceBundle;

public class conPage3 implements Initializable {
    public VBox list;

    public void onAddEvent() {
        daControllers.master.setCenter(daFxml.page3Add);
        daControllers.page3Add.ini();
    }
    public void updateList(){
        list.getChildren().clear();
        new funFiles().readEvents();
        for(moEvent mo: daData.events){
            HBox hb=new HBox();
            hb.getStyleClass().add("HBox");
            Label l1=new Label(mo.getName());
            l1.setPrefWidth(156);
            hb.getChildren().add(l1);
            Label l2=new Label(mo.getCommunication_mode());
            l2.setPrefWidth(96);
            hb.getChildren().add(l2);
            Label l3=new Label(mo.toString2());
            l3.setPrefWidth(460);
            hb.getChildren().add(l3);
            Button delete=new Button("删除");
            delete.setOnAction(e->{
                try {
                    new appAffirmPop("是否确认删除 " + mo.getName() + " 事件?\n删除事件也会删除监听中的事件。", new Runnable() {
                        @Override
                        public void run() {
                            Platform.runLater(() -> {
                                funFiles ff=new funFiles();
                                ff.deleteEvent(mo.getName());
                                for(moListen ml:daData.listens){
                                    for(int i=0;i<ml.getEvents().size();i++){
                                        if(ml.getEvents().get(i).equals(mo.getName())){
                                            ml.getEvents().remove(i);
                                            ml.getAwait_time().remove(i);
                                            i=0;
                                        }
                                    }
                                }
                                ff.saveListens();
                                updateList();
                            });
                        }
                    }).start(new Stage());
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            hb.getChildren().add(delete);
            Button update=new Button("修改");
            update.setOnAction(e->{
                daControllers.master.setCenter(daFxml.page3Add);
                daControllers.page3Add.load(mo);
            });
            hb.getChildren().add(update);
            list.getChildren().add(hb);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Platform.runLater(()->{
        });
    }
}
